<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>产品列表---供应商</title>
    <script type="text/javascript">
      var newProductWin;
      var editProductWin;
      var rejectproductWin;
      var productId;
      Ext.onReady(function(){
        //产品列表---------------------------------------------------------
        //生成有关客户的Grid
        var productDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/product/getProductsOfVendor'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'name'
            }, {
              name: 'model'
            }, {
              name: 'specification'
            }, {
              name: 'manufacturer'
            }, {
              name: 'unit_price'
            }, {
              name: 'brand'
            },{
              name: 'auditing_status'
            },{
              name: 'reject_reason'
            }])
        });
        var productSM = new Ext.grid.CheckboxSelectionModel();
        var productCM = new Ext.grid.ColumnModel([
          new Ext.grid.RowNumberer(),
          productSM,
          {
            header: '产品名称',
            dataIndex: 'name',
            width:180
          }, {
            header: '品牌',
            dataIndex: 'brand',
            width:60
          }, {
            header: '型号',
            dataIndex: 'model',
            width:130
          }, {
            header: '规格',
            dataIndex: 'specification',
            width:80
          }, {
            header: '生产商',
            dataIndex: 'manufacturer',
            width:130
          }, {
            header: '单价',
            dataIndex: 'unit_price',
            sortable:true,
            width:60
          },  {
            header: '审批状态',
            dataIndex: 'auditing_status',
            renderer:auditingRender,
            width:80
          },{
            header:'驳回原因',
            dataIndex:'reject_reason',
            width:80,
            hidden:true
          }]);
        var productGrid =  new Ext.grid.GridPanel({
          ds: productDS,
          cm: productCM,
          sm: productSM,
          width: screenWidth-390,
          height: screenHeight-480,
          iconCls: 'icon-grid',loadMask: true,
          //collapsible: true,
          //animCollapse: false,
          //frame: true,
          //title: '客户列表',
          tbar:['-',{
              text:'添加产品',
              width:50,
              iconCls:'addItem',
              handler:function(){
                newProductWin = new Ext.Window({
                  layout: 'fit',
                  width:617,
                  height:343,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //autoScroll:true,
                  //modal: true,
                  title: '新增产品',
                  autoLoad: {
                    url: "/product/newProduct",
                    scripts: true
                  }
                });
                newProductWin.on("destroy",function(){
                  productDS.reload();
                });
                newProductWin.resizable = false;
                newProductWin.show();
              }
          },{
            text: '编辑产品',
            width:50,
            iconCls:'editItem',
            handler:function(){
                var records = productGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
                }else{
                  //把表单添加到窗口中
                  productId = records[0].get('id');
                  editProductWin = new Ext.Window({
                    title: '编辑产品信息',
                    layout:'fit',
                    width:617,
                    height:448,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    //autoScroll:true,
                    autoLoad: {
                      url: "/product/editProduct?id=" + productId,
                      scripts: true
                    }
                  });
                  editProductWin.on("destroy",function(){
                    productDS.reload();
                  });
                  editProductWin.resizable = false;
                  editProductWin.show();
                }
            }
          },{
              text:'删除产品',
              width:50,
              iconCls: 'remove',
              handler:function(){
                if(productGrid.getSelectionModel().hasSelection()){
                            var rs=productGrid.getSelectionModel().getSelections();
                            var ids=[];
                            var name='';
                            for(var i=0;i<rs.length;i++){
                                var data=rs[i].data;
                                ids.push(data.id);
                                name+=data.name+'<br>';
                            }
                            var keys=Ext.util.JSON.encode(ids);
                            Ext.MessageBox.confirm('警告','确定删除以下产品？<br><font color="red">' + name + '</font>',function (button){
                                if(button=='yes'){
                                    Ext.Ajax.request({
                                        url:'/product/delete',
                                        method:'post',
                                        success:function(response,opts){
                                            var data = Ext.util.JSON.decode(response.responseText);
                                            if(data.success&&data.info=='success') {
                                                 productDS.reload();
                                                 Ext.Msg.alert('提示信息', '已成功删除产品。');
                                            } else {
                                                 Ext.Msg.alert('提示信息', data.info);
                                            }
                                        },
                                        params: {"id":keys}
                                    });
                                }
                            });
                 }else{
                    Ext.Msg.alert('提示信息','请选择要删除的产品');
                 }
              }
            },{
              text: '驳回原因',
              width:50,
              iconCls:'viewItem',
              handler:function(){
                  var records = productGrid.getSelectionModel().getSelections();
                  if(records.length==0){
                    Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再查看。');
                  }else{
                    if(records[0].get('auditing_status')=='驳回'){
                      //把表单添加到窗口中
                      var rejectReason = records[0].get('reject_reason');
                      Ext.MessageBox.alert('驳回原因', rejectReason);
                    }else{
                      Ext.MessageBox.alert('提示信息', '该产品没有被驳回。');
                    }
                    
                  }
              }
          },'-','检索条件','-',
            '产品名称',
            new Ext.form.TextField({
                //fieldLabel: '外勤结束时间',
                name: 'name',
                width:100,
                id: 'name'
             }),
             '审批状态',
             new Ext.form.ComboBox({
                 store: new Ext.data.ArrayStore({
                        fields: ['state'],
                        data : [['通过'],['驳回'],['未审批']] // from states.js
                 }),
                 displayField:'state',
                 typeAhead: true,
                 mode: 'local',
                 id:'state',
                 forceSelection: true,
                 triggerAction: 'all',
                 valueField: 'state',
                 emptyText:'',
                 selectOnFocus:true
             }),
             {
                text:'搜索',
                iconCls:'search',
                handler:function(){
                  productDS.baseParams.name = Ext.get('name').dom.value
                  productDS.baseParams.state = Ext.get('state').dom.value
                  productDS.load({
                    params:{
                      start: 0,
                      limit: pageSize
                    }
                  })
                }
              }
          ],
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: productDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })
        });
        productGrid.render('product_list');
        productDS.load();
        
      })
    </script>
  </head>
  <body>
    <div id="product_list"></div>
  </body>
</html>
